//
//  DNewTagActionView.swift
//  Drop
//
//  Created by hushaohua on 2017/11/16.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DNewTagActionView: UIView, UITextFieldDelegate {
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.addSubview(self.titleLabel);
        self.addSubview(self.cancelButton);
        self.layer.addSublayer(self.separator);
        self.addSubview(self.textField);
        self.addSubview(self.saveButton);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var cancelButton:UIButton = {
        var button:UIButton = UIButton(frame: CGRect(x:self.bounds.size.width - 120,y:0,width:110,height:60));
        button.contentHorizontalAlignment = .right;
        var blueColor = UIColor.h_blue();
        button.setTitleColor(blueColor, for: .normal);
        button.setTitle("取消", for: .normal);
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18);
        button.addTarget(self, action: #selector(cancelButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    
    @objc func cancelButtonClicked(_ sender:UIButton) -> Void {
        self.dismiss();
    }
    
    lazy var titleLabel:UILabel = {
        var label:UILabel = UILabel(frame:CGRect(x:10,y:0,width:200,height:60));
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium);
        label.text = "新建Tag";
        return label;
    }();
    
    lazy var separator:CALayer = {
        var layer:CALayer = CALayer();
        layer.frame = CGRect(x:10,y:60,width:self.bounds.size.width - 20,height:CGFloat(1)/UIScreen.main.scale);
        layer.backgroundColor = UIColor(white:0.5, alpha:0.5).cgColor;
        return layer;
    }();
    
    lazy var textField:UITextField = {
        var field = UITextField(frame:CGRect(x:30,y:90,width:self.bounds.size.width - 60,height:44));
        field.backgroundColor = UIColor(white:0.8, alpha:0.8);
        field.borderStyle = .roundedRect;
        field.delegate = self;
        return field;
    }();
    
    lazy var saveButton:UIButton = {
        var buttonWidth:CGFloat = 160;
        var button = UIButton(frame:CGRect(x: (self.bounds.size.width - buttonWidth) / 2, y: 174, width: buttonWidth, height: 40));
        button.setTitle("保存", for: .normal);
        button.layer.cornerRadius = 3;
        button.setTitleColor(UIColor.white, for: .normal);
        button.backgroundColor = UIColor.h_blue();
        button.addTarget(self, action: #selector(saveButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    @objc func saveButtonClicked(_ sender:UIButton) -> Void {
        var text:String? = self.textField.text;
        text = text?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines);
        if (text != nil && text!.count > 0){
            if (self.mTag == nil){
                let tag = Tag.newInstance();
                tag.name = text!;
            }else{
                let history = TagHistory.newInstance();
                history.name = self.mTag!.name;
                if (self.mTag!.lastUpdatedDate != nil){
                    history.created = self.mTag!.lastUpdatedDate!;
                }else{
                    history.created = self.mTag?.createdDate;
                }
                self.mTag!.name = text!;
                self.mTag!.lastUpdatedDate = Date() as NSDate;
                self.mTag!.addToHistories(history);
            }
            HStoreManager.defaultManger().saveContext();
            self.dismiss();
        }else{
            //震荡textField,并redLabel提示
        }
    }
    
    func dismiss() -> Void {
        let mask = HMaskWindowManager.sharedMaskForView(self);
        if (self.textField.isFirstResponder){
            mask?.keyboardDismissClosure = {() -> () in
                self.textField.resignFirstResponder();
            };
        }else{
            mask?.keyboardDismissClosure = nil;
        }
        HMaskWindowManager.sharedMaskForView(self)?.dismiss();
    }
    func showWithMask() -> Void {
        self.showWithMask(nil);
    }
    
    var mTag:Tag?;
    func showWithMask(_ tag:Tag?) -> Void {
        self.mTag = tag;
        if (tag != nil){
            self.titleLabel.text = "重命名";
            self.textField.text = tag!.name;
        }
        let mask = HMaskWindowManager.sharedMaskForView(self);
        mask?.present();
        mask?.keyboardDismissClosure = {() -> () in
            self.textField.resignFirstResponder();
        };
    }
}
